package class1.s2022350143028.controller;


import base.controller.LoginBaseController;
import class1.s2022350143028.model.UserModel;
import class1.s2022350143028.service.ILoginsxyService;
import class1.s2022350143028.service.Impl.LoginsxyServiceImpl;
import class1.s2022350143028.utils.ValidatesxyUtil;

import java.util.Random;
import java.util.Scanner;
public  class LoginController extends LoginBaseController {

    //声明登录业务逻辑接口
    public ILoginsxyService loginService;

//    public LoginBaseServiceImpl loginImpl;

    //todo 登录方法

    public boolean Login(){
        Random random = new Random();
        int code = random.nextInt(9999);  //验证码产生示例，完成作业需随机生成4位数
        System.out.println("欢迎使用进销存管理系统，请先登录！");
        System.out.println("登录验证码：" + code);

        System.out.println("输入用户名：");
        Scanner scanner = new Scanner(System.in);
        String user = scanner.next();

        System.out.println("输入密码：");
        String pwd = scanner.next();

        System.out.println("输入验证码：");
        int codeInput = scanner.nextInt();

        if(codeInput != code){
            System.out.println("验证码输入错误！");
            //递归调用回checkLogin 直到输入正确
            login();
        }

        if(user.isEmpty() && pwd.isEmpty() && !ValidatesxyUtil.checkUser(user) && !ValidatesxyUtil.checkPassword(pwd)){
            System.out.println("用户名或密码格式错误！");
            //用户名或密码格式错误！
            //输出提示，完成作业自行完善
            //递归调用回checkLogin 直到输入正确用户名密码
            login();
        } else {
            //格式正确，调用登录业务逻辑，检验密码
            UserModel userModel = new UserModel();
            userModel.setUsername(user);
            userModel.setPassword(pwd);
            loginService = new LoginsxyServiceImpl();
            return loginService.CheckLogin(userModel);
        }


        return false;
    }

    @Override
    public void login() {

    }



}
